{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        vowels = \"aeiouAEIOU\"\n",
    "        vowels_pair = []\n",
    "        vowels_list = []\n",
    "        left = 0\n",
    "        right = 0\n",
    "        l = len(s)\n",
    "        while right < l:\n",
    "            while left < l and s[left] not in vowels:\n",
    "                left = left + 1\n",
    "            if left == l:\n",
    "                break\n",
    "                \n",
    "            right = left\n",
    "            while right < l and s[right] in vowels:\n",
    "                vowels_list.append(s[right])\n",
    "                right = right + 1\n",
    "\n",
    "            vowels_pair.append((left, right))\n",
    "            \n",
    "            left = right\n",
    "            \n",
    "        vowels_list[:] = vowels_list[::-1]\n",
    "        \n",
    "        cur = 0\n",
    "        s = list(s)\n",
    "        for left, right in vowels_pair:\n",
    "            s[left:right] = vowels_list[cur:cur+(right-left)]\n",
    "            cur = cur + right - left\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Aa'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.reverseVowels('aA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
